package cn.bytecollege.dao;

import cn.bytecollege.entity.Admin;

import java.util.List;

/**
 * 管理员DAO
 * @author qy
 */
public class AdminDao extends BaseDao<Admin,Integer>{

    /**
     * 根据管理员名字查询管理员信息
     * @param adminName
     * @return qy
     */
    public Admin findByAdminName(String adminName){
        String sql = "select admin_id,admin_name,admin_password from admin where admin_name = ?";
        List<Admin> list = helper.executeQuery(sql,Admin.class,adminName);
        Admin admin = null;
        if (list.size()>0){
            admin = list.get(0);
        }
        return admin;
    }

    /**
     * 添加管理员
     * @param admin
     * @return qy
     */
    @Override
    public int save(Admin admin) {
        String sql = "insert into admin (admin_name,admin_password) value (?,?)";
        Object[] objects = {admin.getAdminName(),admin.getAdminPassword()};
        return helper.executeUpdate(sql,objects);
    }

    /**
     * 根据管理员ID更改管理员名和密码
     * @param admin
     * @return qy
     */
    @Override
    public int update(Admin admin) {
        String sql = "update admin set admin_name = ?,admin_password = ? where admin_id = ?";
        Object[] objects = {admin.getAdminName(),admin.getAdminPassword(),admin.getAdminId()};
        return helper.executeUpdate(sql,objects);
    }

    /**
     * 根据管理员ID删除管理员
     * @param adminId
     * @return qy
     */
    @Override
    public int deleteById(Integer adminId) {
        String sql = "delete from admin where admin_id = ?";
        return helper.executeUpdate(sql,adminId);
    }
}

